<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="视频地址" prop="video">
        <el-input
          v-model="queryParams.video"
          placeholder="请输入视频地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图片地址" prop="imgUrl">
        <el-input
          v-model="queryParams.imgUrl"
          placeholder="请输入图片地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yh:notice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yh:notice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yh:notice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yh:notice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="类型" align="center" prop="typeText" />
      <el-table-column label="视频名" align="center" prop="videoName" />
      <el-table-column label="图片名" align="center" prop="imgName" />
      <el-table-column label="详情" align="center" prop="details" :show-overflow-tooltip="true" min-width="120"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['yh:notice:edit']">修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yh:notice:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改各类型公告展示对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频地址" prop="video" v-if="form.type == 'TUTORIAL'">
          <!-- <el-input v-model="form.video" placeholder="请输入视频地址" /> -->
          <el-upload class="upload-demo" :action="`${base}/common/upload`"
            :on-preview="handlePreview" :on-remove="handleRemove" :on-success="onSuccess" :before-remove="beforeRemove"  :limit="1"
            :on-exceed="handleExceed" :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="图片地址" prop="imgUrl"  v-if="form.type == 'INTRODUCTION'">
          <el-upload
            style="width: 200px;height: 200px;margin-bottom: 40px;"
            :on-success="pictureSuccess"
            :action="`${base}/common/upload`"
            :show-file-list="false"
            :on-remove="pictureRemove">
            <img  v-if="dialogImageUrl" width="100%" :src="dialogImageUrl" alt="">
            <i v-else class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="详情" prop="details">
          <el-input v-model="form.details" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/yh/notice";
  import baseUrl from "@/api/baseUrl";

  export default {
    name: "Notice",
    data() {
      return {
        base: process.env.VUE_APP_BASE_API,
        fileList: [{name:'',url:''}],
        dialogImageUrl :'',
        // 遮罩
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 各类型公告展示表格数据
        noticeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: null,
          video: null,
          imgUrl: null,
          details: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      pictureRemove(file, fileList) {
        console.log(file, fileList);
      },
      pictureSuccess(response){
        console.log(response,'xxx')
        this.dialogImageUrl = response.url;
        this.form.imgUrl = response.fileName;
        this.form.imgName = response.originalFilename;
      },
      onSuccess(response){
        this.form.video = response.fileName
        this.form.videoName = response.originalFilename
        console.log(this.form,'video',response)
      },
      handleRemove(file, fileList) {
        if(file.status == 'success'){
          this.form.video = ''
          this.form.videoName = ''
        }
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },


      /** 查询各类型公告展示列表 */
      getList() {
        this.loading = true;
        listNotice(this.queryParams).then(response => {
          this.noticeList = response.rows.map(item => {
            switch (item.type) {
              case 'HOME_NOTICE':
                item.typeText = '首页公告'
                break;
              case 'TUTORIAL':
                item.typeText = '扫码教程'
                break;
              case 'WITHDRAWAL_RULES':
                item.typeText = '提现规则'
                break;
              case 'INTRODUCTION':
                item.typeText = '行业介绍'
                break;
              default:
                break;
            }
            return {
              ...item,
              video: item.video ? item.video : '暂无',
              imgUrl: item.imgUrl ? item.imgUrl : '暂无'
            }
          });

          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          type: null,
          video: null,
          imgUrl: null,
          details: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加各类型公告展示";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        if(row.type=="TUTORIAL" && row.videoName != '')this.fileList[0].name = row.videoName
        this.reset();
        const id = row.id || this.ids
        getNotice(id).then(response => {
          this.form = response.data;
          console.log(this.form,'this.form')
          this.dialogImageUrl = baseUrl + this.form.imgUrl;
          console.log(this.dialogImageUrl,'dialogImageUrl')
          // this.fileList =[]
          this.open = true;
          this.title = "修改各类型公告展示";
        });
      },
      /** 提交按钮 */
      submitForm() {
        // console.log(this.form,'提交')
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              console.log(this.form,'提交2')
              updateNotice(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addNotice(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除各类型公告展示编号为"' + ids + '"的数据项？').then(function () {
          return delNotice(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('yh/notice/export', {
          ...this.queryParams
        }, `notice_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
<style>
  .el-tooltip__popper {
 
 max-width: 600px;

}
</style>

